package com.example.demo.common;

import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ExceptionCode;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/10/26 0026.
 */
public class ResponseBack<T> implements Serializable {

    private int code;
    private String msg;
    private T t;
    private Boolean success;

    public ResponseBack(BusinessException e) {
        this.code = e.getCode();
        this.msg = e.getMsg();
        this.t = null;
        this.setSuccess(checkSuccess(code));
    }

    public ResponseBack(Exception e) {
        this.code = ExceptionCode.SYSTEM_ERROR.getCode();
        this.msg = ExceptionCode.SYSTEM_ERROR.getMsg();
        this.t = null;
        this.setSuccess(checkSuccess(code));
    }

    public ResponseBack(T t) {
        this.code = 0;
        this.msg = null;
        this.t = t;
        this.setSuccess(checkSuccess(code));
    }


    public static <T> ResponseBack<T> error(BusinessException e) {
        return new ResponseBack(e);
    }

    public static <T> ResponseBack<T> error(Exception e) {
        return new ResponseBack(e);
    }

    public static <T> ResponseBack<T> success(T t) {
        return new ResponseBack(t);
    }


    private Boolean checkSuccess(int code) {
        return code == 0 ? true : false;
    }



    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return success;
    }




}
